import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
import cv2
import os
from cv_bridge import CvBridge

class ImagePublisher(Node):
    def __init__(self):
        super().__init__("image_publisher")
        
        self.declare_parameter('img_path', "./img.png")
        self.img_path = self.get_parameter("img_path").value
        
        self.pub_ = self.create_publisher(Image, 'image_topic', 10)
        self.timer_ = self.create_timer(0.1, self.pub_img)
        self.bridge_ = CvBridge()
        
    def pub_img(self):
        if os.path.exists(self.img_path):
            img = cv2.imread(self.img_path)
            img_msg = self.bridge_.cv2_to_imgmsg(img, encoding='bgr8')
            self.pub_.publish(img_msg)
        else:
            self.get_logger().warn("Image not found")
            
def main(args=None):
    rclpy.init(args=args)
    img_pub = ImagePublisher()
    rclpy.spin(img_pub)
    img_pub.destroy_node()
    rclpy.shutdown()
    
if __name__ == "__main__":
    main()